package com.lxp.userms.service.impl;

import com.lxp.userms.entity.Favorite;
import com.lxp.userms.mapper.FavoriteMapper;
import com.lxp.userms.service.FavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class FavoriteServiceImpl implements FavoriteService {

    @Autowired
    private FavoriteMapper favoriteMapper;

    @Override
    @Transactional
    public int addFavorite(Favorite favorite) {
        return favoriteMapper.insertFavorite(favorite);
    }

    @Override
    @Transactional
    public int removeFavorite(int postId, int userId) {
        return favoriteMapper.deleteFavorite(postId, userId);
    }

    @Override
    @Transactional
    public void deleteFavoritesByPostId(int postId) {
        favoriteMapper.deleteFavoritesByPostId(postId);
    }

    @Override
    public int getFavoriteCount(int postId) {
        return favoriteMapper.countFavoritesByPostId(postId);
    }

    @Override
    public boolean isFavorited(int postId, int userId) {
        Favorite favorite = favoriteMapper.checkFavorite(postId, userId);
        return favorite != null;
    }
}